﻿Imports System.Data.OleDb

Public Class DAODiem

    Public Function Connection(ByVal path As String) As OleDbConnection
        Return New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path)
    End Function
    Public Function LoadMa(ByVal MaHS As Long, ByVal path As String) As DataTable
        Dim dset As DataSet = New DataSet
        Dim strQuery As String = "Select * from HocSinh where MaHS=" + MaHS
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        conn.Close()
        ad.Fill(dset, "HocSinh")
        Return dset.Tables("HocSinh")
    End Function
    Public Function LoadTable(ByVal strQuery As String, ByVal path As String) As DataTable
        Dim dset As DataSet = New DataSet
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        conn.Close()
        ad.Fill(dset, "Diem")
        Return dset.Tables("Diem")
    End Function
    Public Function Insert(ByVal diem As DTODiem, ByVal path As String) As Boolean
        Dim strQuery As String = "Insert into Diem(MaHS,MaMonHoc,HocKy,NamHoc,Diem15Phut,Diem1Tiet,DiemCuoiKy) values(" + diem.MaHS.ToString() + "," + diem.MaMonHoc.ToString() + "," + diem.HocKy.ToString() + "," + diem.NamHoc.ToString() + "," + diem.Diem15Phut.ToString() + "," + diem.Diem1Tiet.ToString() + "," + diem.DiemCuoiKy.ToString() + ")"
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim flag As Boolean = False
        Try
            flag = cmd.ExecuteNonQuery() > 0
        Catch ex As Exception
            flag = False
        End Try

        conn.Close()
        Return flag
    End Function
    Public Function Delete(ByVal diem As DTODiem, ByVal path As String) As Boolean
        Dim strQuery As String = "Delete  from Diem where MaHS=" + diem.MaHS + " and MaMonHoc=" + diem.MaMonHoc + " and HocKy=" + diem.HocKy + " and NamHoc=" + diem.NamHoc
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim flag As Boolean = False
        Try
            flag = cmd.ExecuteNonQuery() > 0
        Catch ex As Exception
            flag = False
        End Try
        conn.Close()
        Return flag
    End Function
    Public Function Update(ByVal diem As DTODiem, ByVal path As String) As Boolean
        Dim strQuery As String = "Update Diem Set Diem15Phut=" + diem.Diem15Phut.ToString() + ", Diem1Tiet=" + diem.Diem1Tiet.ToString() + ", DiemCuoiKy=" + diem.DiemCuoiKy.ToString() + " where MaHS=" + diem.MaHS.ToString() + " and MaMonHoc=" + diem.MaMonHoc.ToString() + " and HocKy=" + diem.HocKy.ToString() + " and NamHoc=" + diem.NamHoc.ToString()
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim flag As Boolean = False
        Try
            flag = cmd.ExecuteNonQuery() > 0
        Catch ex As Exception
            flag = False
        End Try
        conn.Close()
        Return flag
    End Function
End Class
